﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LaySprinklerBasement : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException();
				}
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
				{
					(ViewType)1,
					(ViewType)2,
					(ViewType)115
				}))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					IntPtr mainWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
					this._hWndRevit = new YJKWindowHandle(mainWindowHandle);
					YJKRevitTools.GetFloorHeigh(document, activeUIDocument.ActiveView);
					new HYEquipmentCommon();
					new RecSprinklerHelper(document);
					SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
					new LaySprinklerHelper(document);
					FormlaySprinklerBasement formlaySprinklerBasement = new FormlaySprinklerBasement(document);
					if (formlaySprinklerBasement.ShowDialog() != DialogResult.OK)
					{
						result = Autodesk.Revit.UI.Result.Succeeded;
					}
					else
					{
						LayBasementManage layBasementManage = new LayBasementManage(commandData, formlaySprinklerBasement.layParameter);
						FamilySymbol familySymbolById = spaceFamilyDataReader.GetFamilySymbolById(1, "Sprinkler", "DN15", document, BuiltInCategory.OST_Sprinklers);
						if (familySymbolById == null)
						{
							result = Autodesk.Revit.UI.Result.Succeeded;
						}
						else
						{
							SprinklerModelProcess sprinklerModelProcess = new SprinklerModelProcess(commandData, this._hWndRevit);
							while (layBasementManage.Run())
							{
								if (!sprinklerModelProcess.Go(familySymbolById, layBasementManage))
								{
									return Autodesk.Revit.UI.Result.Succeeded;
								}
								YJKMessageBox.Show("地库喷头布置完成：\n已在最大主梁矩形包围圈内布置了喷头");
								if (formlaySprinklerBasement.layParameter.SelectType != 1)
								{
									return Autodesk.Revit.UI.Result.Succeeded;
								}
							}
							YJKMessageBox.Show("布置失败：\n" + layBasementManage.szError);
							result = Autodesk.Revit.UI.Result.Succeeded;
						}
					}
				}
			}
			catch (Exception ex)
			{
				if (ex.ToString().Contains("您取消了操作！"))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					string message2 = ex.Message;
					result = Autodesk.Revit.UI.Result.Failed;
				}
			}
			return result;
		}

		public YJKWindowHandle _hWndRevit;
	}
}
